package Survey;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.util.Vector;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class CModuleText extends CreatorModule{

	private static final long serialVersionUID = 6752125623933088177L;
	JTextField size;
	
	public CModuleText(SurveyParameters data, Vector<Boolean> showing){
		super(data, showing);
		size = new JTextField(data.dimensions[0] == 0 ? "":"" + data.dimensions[0], 6);
		size.addFocusListener(new FocusListener(){
			public void focusGained(FocusEvent e) {return;}
			public void focusLost(FocusEvent e) {
				try{
					((CModuleText)((Component) e.getSource()).getParent().getParent().getParent()).data.dimensions[0] = 
						Integer.parseInt(((JTextField) e.getSource()).getText().trim());
					((JTextField)e.getSource()).setBackground(Color.white);
				}catch(NumberFormatException ex){
					((JTextField)e.getSource()).setText(
						(((CModuleText)((Component) e.getSource()).getParent().getParent().getParent()).data.dimensions[0]) == 0 ?"":
							"" + ((CModuleText)((Component) e.getSource()).getParent().getParent().getParent()).data.dimensions[0]);}
			}
		});
		
		JPanel main2 = new JPanel();
		main2.setBackground(Color.white);
		main2.setOpaque(true);
		
		main2.add(new JLabel("Number of Lines: "));
		main2.add(size);
		main.add(main2);
		totalSize = new Dimension(SurveyCreatorGui.WIDTH - 20, 100);
		setPreferredSize(totalSize);
		setMaximumSize(getPreferredSize());
		
		if(!showing.get(data.index - 1).booleanValue()){
			main.setVisible(false);
			visible.setText("Show");
			setPreferredSize(SurveyCreatorGui.TOPSIZE);
			setMaximumSize(getPreferredSize());
		}
		
		setVisible(true);
	}

	public void highlight(int i) {
		if(i%2 == 1){
			question.setBackground(Color.red);
		}else{
			question.setBackground(Color.white);
		}
		i /= 2;
		if(i%2 == 1){
			size.setBackground(Color.red);
		}else{
			size.setBackground(Color.white);
		}
		validate();
		repaint();
	}

}
